//
//  HOAddQuestionViewController
//  WeiDu_1_0
//
//  Created by foguo-mac-1 on 01/07/16.
//  Copyright (c) 2016 WeiDu Energy Tech. All rights reserved.
//

#import "HOAddQuestionViewController.h"
#import "Logger.h"
#import "HOUtils.h"
#import "HOConstants.h"
#import "HOEditView.h"
#import "HODefine.h"
#import "HOQuestionTypeViewController.h"
@interface HOAddQuestionViewController()<HOQuestionTypeViewControllerDelegate>
{
	UIButton *selectTypeBtn;
}
@property(nonatomic, assign)HO_QUESTION_TYPE questionType;
@property(nonatomic,retain)NSString *typeTitle;
@end

@implementation HOAddQuestionViewController

- (id)initWithModule:(HO_MODULE_TYPE)type
{
	return [super initWithModule:type];
}

- (void)sendNow:(HOEditView *)endView
{
}
//@Override
- (void)viewDidLoad
{
	[super viewDidLoad];
	self.inputBoxHeight = 80;
	self.questionType = QUESTION_TYPE_OTHER;
	
	UIView *view = [[UITextField alloc]
				initWithFrame:CGRectMake(-1,
										 20,
										 self.view.frame.size.width + 2,
										 40)];
	view.layer.borderWidth = 1;
	[view setBackgroundColor:[UIColor whiteColor]];
	view.layer.borderWidth = [HOUtils  defaultOnePixelConversion];
	view.layer.borderColor = UIColorFromRGB(0xdcdcdc).CGColor;
	
//	titleLbl.layer.cornerRadius = 5;
	[self.view addSubview:view];
	
	selectTypeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
	selectTypeBtn.frame = CGRectMake(10, 20 + [HOUtils  defaultOnePixelConversion], self.view.frame.size.width - 20, 40 - 2 * [HOUtils  defaultOnePixelConversion]);
//	[selectTypeBtn setTitle:NSLocalizedString(@"选择问题类型", @"选择问题类型") forState:UIControlStateNormal];
	
	[self updateTypeSelectLabel];
	[selectTypeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
	[selectTypeBtn addTarget:self action:@selector(selectQuestionType:) forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:selectTypeBtn];
	[selectTypeBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
	[selectTypeBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
//	selectTypeBtn.layer.borderWidth = [HOUtils  defaultOnePixelConversion];
	
	UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 40, 30, 20, 20)];
	imgView.image = [UIImage imageNamed:@"icon_go"];
	[self.view addSubview:imgView];
	[imgView release];
	
	inputBox = [[HOEditView alloc]
				initWithFrame:CGRectMake(0,
										 80,
										 self.view.frame.size.width,
										 self.inputBoxHeight)];
	oneLineView = [[UIView alloc]
				   initWithFrame:CGRectMake(-1,
											20,
											self.view.frame.size.width + 2,
											self.inputBoxHeight)];
	[oneLineView setBackgroundColor:[UIColor whiteColor]];
	oneLineView.layer.borderWidth  = 1;
	oneLineView.layer.borderColor = UIColorFromRGB(0xcdcdcd).CGColor;
	oneLineInputBox = [[UITextField alloc]
					   initWithFrame:CGRectMake(HO_HORIZONAL_PADDING,
												0,
												self.view.frame.size.width - HO_HORIZONAL_PADDING * 2,
												self.inputBoxHeight)];
	
	oneLineInputBox.textColor = UIColorFromRGB(0x666000);
	oneLineInputBox.clearButtonMode = UITextFieldViewModeWhileEditing;
	oneLineInputBox.font = [UIFont systemFontOfSize:15];
	oneLineInputBox.delegate = self;
	inputBox.placeHolder = NSLocalizedString(@"请输入问题内容", @"请输入问题内容");
	//    oneLineInputBox.layer.borderWidth = 1;
	inputBox.limitedCount = 200;
	if(self.needHideLimitedLbl)
		[inputBox hideLimitedCountView];
	[self.view addSubview:inputBox];
	[oneLineView addSubview:oneLineInputBox];
	[self.view addSubview:oneLineView];
	[inputBox getTextView].font = [UIFont systemFontOfSize:15];
	
	
	
	
	inputBox.hidden = NO;
	[[inputBox getTextView] becomeFirstResponder];
	oneLineView.userInteractionEnabled = NO;
	oneLineView.hidden = YES;
	
	[self registerTapGesture];
	
}

- (void)selectQuestionType:(id)sender
{
	HOQuestionTypeViewController *viewCtrl = [[HOQuestionTypeViewController alloc] init];
	viewCtrl.delegate = self;
	[self.navigationController pushViewController:viewCtrl animated:YES];
	[viewCtrl release];
}

- (void)hideKeyboard
{
	[inputBox resignFirstResponder];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//	if([self.fieldName isEqualToString:kHO_FIELD_TelPhone])
//	{
//		NSString *s = [textField.text stringByReplacingCharactersInRange:range withString:string];
//		NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^[0-9-]+$" options:0 error:nil];
//		NSTextCheckingResult *match = [regex firstMatchInString:s options:0 range:NSMakeRange(0, [s length])];
//		return (match != nil);
//	}
//	else
//	{
//		return YES;
//	}
	return YES;
}
- (void)hideLimitedLbl
{
	
	[inputBox hideLimitedCountView];
}

//@Override
- (void)viewWillAppear:(BOOL)animated
{
	self.navigationController.navigationBar.hidden = NO;
	self.tabBarController.tabBar.hidden = YES;
	[self setNavigationItemTitle:NSLocalizedString(@"提问", @"提问")];
	[super viewWillAppear:animated];
	[super registerKeyboard];
	
	[self displayRightButtomItemWithTitle:NSLocalizedString(@"提交", @"提交")];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
	[super unregisterKeyboard];
}

- (void)hideHintView
{
	[super hideHintView];
}

- (void)submitContent
{
	
//	NSString *title = [titleLbl.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

	NSString *content = [[inputBox getText] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
	if(content == nil || content.length == 0)
	{
		[self popupHintView:NSLocalizedString(@"请填写问题内容", @"请填写问题内容")];
		return;
	}
	[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_AddQuestion,
								  kHO_FIELD_Title:content,
								  kHO_FIELD_Type:[NSNumber numberWithInt:self.questionType]}];
	
}

//@Override
- (void)rightButtonItemDidClick
{
	[self submitContent];
}

//@Override
- (void)didReceiveResponse:(HOBaseResponseEntity *)response
{
	if(![response.action isEqualToString:kHO_ACTION_AddQuestion])
		return;
	[super didReceiveResponse:response];
	if([response isRequestDone])
	{
		if([response.action isEqualToString:kHO_ACTION_AddQuestion])
		{
			[self popupHintView:NSLocalizedString(@"修改成功!", @"修改成功!") inTime:1 needCallback:YES];
			self.view.userInteractionEnabled = NO;
		}
	}
	
}

- (void)didSelectQuestionType:(HO_QUESTION_TYPE)type withTypeContent:(NSString *)typeContent
{
	self.questionType = type;
	self.typeTitle = typeContent;
	[self updateTypeSelectLabel];

}

- (void)updateTypeSelectLabel
{
	NSString *content = NSLocalizedString(@"选择问题类型", @"选择问题类型");
	if(self.typeTitle != nil)
	{
		content = [NSString stringWithFormat:@"%@    %@", NSLocalizedString(@"已经选择问题类型:", @"已经选择问题类型:"), self.typeTitle];
	}
	[selectTypeBtn setTitle:content forState:UIControlStateNormal];
}

#pragma mark
//@Override
- (void)hintDidHide
{
	self.view.userInteractionEnabled = YES;
	[self goBack];
}
//@Override
- (void)dealloc
{
	[HOUtils removeFromSuperViewForViews:oneLineInputBox, inputBox];
	[oneLineInputBox release];
	[inputBox release];
	
	[super dealloc];
}
@end
